package com.shixun.dao;

import com.opensymphony.xwork2.ActionContext;
import com.shixun.entity.FileEntity;
import com.shixun.entity.FpinglunEntity;
import com.shixun.entity.UserEntity;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.annotations.common.util.impl.Log;
import org.jcp.xml.dsig.internal.SignerOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.List;

/**
 * Created by fan-computer on 2016/12/13.
 */
@Repository("FcbDao")
public class FcbDaoImpl extends BaseDao implements FcbDao {

    @Autowired
    private FpinglunEntity fpinglunEntity;

    /**
     * 用户注册
     * 向UserEntity中增加一条数据
     *
     * @param userEntity
     * @return
     * @throws Exception
     */
    @Override
    public void addForBuser(UserEntity userEntity) {
        userAdd(userEntity);
    }

    /**
     * 用户登陆
     *
     * @param string
     * @return
     */
    @Override
    public List<Object> serchUser(String string) {
        return userSelect("from UserEntity where loginId ='" + string + "'");
    }


    /**
     * 发帖子
     *
     * @param fileEntity
     */
    @Override
    public void postTiezi(FileEntity fileEntity) {
        userAdd(fileEntity);
    }


    /**
     * 查找帖子，获得帖子内容简介（标题）
     *
     * @param
     * @return
     */
    @Override
    public List<Object> searchTiezi() {
        return userSelect("from FileEntity where more1='" + "0" + "'");
    }


    /**
     * 删除帖子
     *
     * @param fileEntity
     * @return
     */
    @Override
    public boolean delete_tiezi(FileEntity fileEntity) {
//        deleteId(fileEntity);
        return true;
    }

    @Override
    public List<Object> searchFileDao() {
        return userSelect("from FileEntity where more1='" + "0" + "'");
    }

    /**
     * 获取帖子的具体内容
     *
     * @param str
     * @return
     */
    @Override
    public List<Object> searchOneTiezi(String str) {

        return userSelect("from FileEntity where id ='" + str + "'");
    }


    /**
     * 通过id查找一条信息（从评论表里查）
     *
     * @param string
     * @return
     */
    @Override
    public List<Object> searchPinglun(String string) {
        return userSelect("from FpinglunEntity where fileEntity.id ='" + string + "'");
    }

    /**
     * 点赞
     *
     * @param string
     */
    @Override
    public void saveDianzan(String string) {
        fpinglunEntity.setMore1("00");//00表示帖子点赞
        fpinglunEntity.setFileEntity((FileEntity) searchOneTiezi(string).get(0));
        fpinglunEntity.setUserEntity((UserEntity) getSessions().get("user"));
        fpinglunEntity.setState("1");
        fpinglunEntity.setFtime(new Timestamp(System.currentTimeMillis()));
        fpinglunEntity.setCreatetime(new Timestamp(System.currentTimeMillis()));
        userAdd(fpinglunEntity);
    }


    /**
     * 发表评论
     *
     * @param s1
     * @param s2
     */
    @Override
    public void tieziPinglun(String s1, String s2) {
        fpinglunEntity.setMore1("00");   //对文章的留言区分
        fpinglunEntity.setFmain(s2);
        fpinglunEntity.setFileEntity((FileEntity) searchOneTiezi(s1).get(0));
        fpinglunEntity.setUserEntity((UserEntity) BaseDao.getSessions().get("user"));
        fpinglunEntity.setFtime(new Timestamp(System.currentTimeMillis()));
        fpinglunEntity.setCreatetime(new Timestamp(System.currentTimeMillis()));
        fpinglunEntity.setState("1");
        userAdd(fpinglunEntity);
    }


    /**
     * 查询发表的评论
     *
     * @param s1
     * @return
     */
    @Override
    public List<Object> findPinglun(String s1) {
        return userSelect("from FpinglunEntity where fileEntity.id = '" + s1 + "'");
    }




  /*  *//**
     * 查询帖子的评论
     * @param s
     * @return
     *//*
    @Override
    public List<Object> getPinglunDao(String s) {
        return userSelect("from FpinglunEntity where fileEntity.id ='" + s + "'");
    }*/



    /**
     * 后台管理中心查看文章
     */
    @Override
    public List<Object> findArticle(String string) {

         return userSelect("from FileEntity where id='" + string + "'");
    }



    /**
     * 后台管理中心删除文章（通过文章ID删除）
     *
     * @param s
     */
    @Override
    public void deleteArticleDao(String s) {
        deleteId("delete from FileEntity where id='" + s + "'");
    }


    /**
     * 后台管理中心查看帖子
     * @param string
     * @return
     */
    @Override
    public List<Object> findTiezi(String string) {
        return userSelect("from FileEntity where id='"+string+"'");
    }


    /**
     * 后台管理中心删除帖子
     * @param s
     */
    @Override
    public void deleteTieziDao(String s) {
        deleteId("delete from FileEntity where id='"+s+"'");

    }

}
